package com.ybfenpan.controller.admin.center;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.NoticeService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;

/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class CenterAddController extends BaseController {
	@Resource
	private NoticeService noticeService;

	@Resource
	private MemberService memberService;

	@RequestMapping("admin/center/centerAdd.shtml")
	public String add(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map formMap = getParameterMap(request);
		String loginName = MapUtil.getString(formMap, "loginName");
		boolean result = true;

		Member one = memberService.executMemberByLoginName(loginName);

		// 账号
		if (one != null) {
			if (one.getAccountCenter().equals("T")) {
				setMessage(model, "该代理商已经是服务中心了");
				result = false;
			}

		} else {
			System.out.println("代理商编号不正确!");
			setMessage(model, "代理商编号不正确。");
			result = false;
		}

		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");

		if (myTwoPassword == null || myTwoPassword.length() < 1) {
			setMessage(model, "安全密码未填写！");
			result = false;
		} else {
			Admin admin = ContextUtil.getAdmin();
			String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword, CryptUtil.keyStr);

			if (!admin.getTwoPassword().equals(pwd)) {
				setMessage(model, "管理员安全密码不正确。");
				result = false;
			}
		}

		if (!result) {
			return "admin/center/centerAdd";
		} else {
			if (one.getAccountCenter().equals("F")) {
				one.setAccountCenter("T");
				this.memberService.update(one);
			}
		}

		return "redirect:centerList.shtml";

	}

}
